package org.jeecg.modules.demo.template.service.impl;

import org.jeecg.modules.demo.template.entity.BwDj;
import org.jeecg.modules.demo.template.entity.BwDjlb;
import org.jeecg.modules.demo.template.mapper.BwDjlbMapper;
import org.jeecg.modules.demo.template.mapper.BwDjMapper;
import org.jeecg.modules.demo.template.service.IBwDjService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 单价表
 * @Author: jeecg-boot
 * @Date:   2024-11-11
 * @Version: V1.0
 */
@Service
public class BwDjServiceImpl extends ServiceImpl<BwDjMapper, BwDj> implements IBwDjService {

	@Autowired
	private BwDjMapper bwDjMapper;
	@Autowired
	private BwDjlbMapper bwDjlbMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BwDj bwDj, List<BwDjlb> bwDjlbList) {
		bwDjMapper.insert(bwDj);
		if(bwDjlbList!=null && bwDjlbList.size()>0) {
			for(BwDjlb entity:bwDjlbList) {
				//外键设置
				entity.setDjId(bwDj.getId());
				bwDjlbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BwDj bwDj,List<BwDjlb> bwDjlbList) {
		bwDjMapper.updateById(bwDj);
		
		//1.先删除子表数据
		bwDjlbMapper.deleteByMainId(bwDj.getId());
		
		//2.子表数据重新插入
		if(bwDjlbList!=null && bwDjlbList.size()>0) {
			for(BwDjlb entity:bwDjlbList) {
				//外键设置
				entity.setDjId(bwDj.getId());
				bwDjlbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		bwDjlbMapper.deleteByMainId(id);
		bwDjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bwDjlbMapper.deleteByMainId(id.toString());
			bwDjMapper.deleteById(id);
		}
	}
	
}
